package com.lxf.canal.api.demo;

import com.alibaba.fastjson.JSON;
import com.lxf.canal.api.base.BaseDataConstant;
import com.lxf.canal.api.utils.RSAUtil;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName RSA
 * @Description RSA相关操作
 * @Date 2025/5/26 15:20
 * @Version 1.0
 **/
public class RSAMain {
    /**
     * @description: 生成RSA密钥信息
     * @date: 2025/5/26 15:06
     */
    @Test
    public void genRSAKey( )throws Exception{
        //生成公私钥信息
        Map<String, Object> map = RSAUtil.genKeyPair();
        String publicKey = RSAUtil.getPublicKey(map);
        System.out.println("商户公钥publicKey:"+publicKey);
        String privateKey = RSAUtil.getPrivateKey(map);
        System.out.println("商户私钥privateKey:"+privateKey);
    }
    /**
     * @description: 测试加解密
     * @date: 2025/5/26 15:06
     */
    @Test
    public void testEncrDecr( )throws Exception{
        Map<String, String> reqMap = new HashMap<>();
        reqMap.put("name","张三");
        reqMap.put("age","30");
        //公钥加密数据(如果是请求平台方，需要用平台方公钥加密)
        String encrData = RSAUtil.encryptPub(JSON.toJSONString(reqMap), BaseDataConstant.COMPANY_PUBLIC_KEY);
        System.out.println("公钥加密数据===>" + encrData);
        //私钥解密数据
        String decrData = RSAUtil.decryptPri(encrData, BaseDataConstant.COMPANY_PRIVATE_KEY);
        System.out.println("私钥解密数据===>" + decrData);
    }
}
